import { getSelection, batchUpdateValue,fireExecute, sdp_language_sql_extra_info } from '/src/utils/index'
import { ref, unref, watch, onMounted } from 'vue'
export function init(self) {
  const module = self.module.value
  self.extra_info_columns = sdp_language_sql_extra_info.filter(ele => ele.label!='==')

  self.custom = {
    columns : [
      {
        prop: 'workspace_name',
        order: 'asc',
        width: 150,
      },
      {
        prop: 'table_name',
        order: 'asc',
        width: 150,
      },
      {
        prop: 'parameter_catalog_type',
        order: 'asc',
        width: 250,
      },
      {
        prop: 'parameter_catalog',
        order: 'asc',
        width: 110,
      },
      {
        prop: 'sort_no',
        order: 'asc',
        width: 100,
      },
      {
        prop: 'name',
        order: 'asc',
        width: 200,
      },
      {
        prop: 'java_type',
        width: 200,
      },
      {
        prop: 'remarks',
        width: 100,
      },
      {
        prop: 'parameter_is_like',
        width: 100,
      },
      {
        prop: 'parameter_sql_value',
        width: 100,
      },
      {
        prop: 'parameter_without_test',
        width: 100,
      },
      {
        prop: 'parameter_sql',
        width: 100,
      },
      {
        prop: 'parameter_nullable',
        width: 100,
      },
      {
        prop: 'parameter_sql_value_ignore',
        width: 100,
      },
      {
        prop: 'java_imports',
        width: 100,
      },
      {
        prop: 'java_return_type',
        width: 100,
      },
      {
        prop: 'is_interface',
        width: 100,
      },
      {
        prop: 'parameter_is_import_excel',
        width: 100,
      },
      {
        prop: 'parameter_is_export_excel',
        width: 100,
      },
      {
        prop: 'extra_info',
        width: 100,
      },
      ...self.extra_info_columns.map(ele => {
        return {
          ...ele,
          prop: 'extra_info_'+ele.prop,
          width: 100,
        }
      }),
      {

      }
    ],

    editColumns : [
      {
        prop: 'workspace_name',
        label: '工作区',
        list_remote: {
          module: 'sdp_workspace', method:'list',
          query: {},
          sortInfo: ['name'],
          mapInfo: {label:'name', value:'name'}
        },
        type: 'select',
        required: true,
        onchange: param => {
          let {mode, form, column, columns, get_remote_list, set_query_storage} = param
          get_remote_list({...param, columns,
            prop: 'table_name', query: {workspace_name: form[column.prop]}})
          .then(param => {
            const { data } = param
            if (data && data.length) {
              let column = columns.find(ele => ele.prop == 'table_name')
              column.list.forEach( ele => {
                if (ele.data) {
                  if (ele.data.alias_name) {
                    ele.label += ' as '+ele.data.alias_name
                  }
                  let remark = ele.data.remark
                  if (remark) {
                    remark += ''
                    remark = remark.split('\n')[0].split('\r')[0].trim()
                    if (remark.length > 15) {
                      remark = remark.substring(0, 15)+'...'
                    }
                    ele.label += ' ('+remark+')'
                  }
                }
              })
              if (form.table_name && !data.find(ele => ele.name == form.table_name)) {
                form.table_name = ''
              }
            }
          })
        },
      },
      {
        prop: 'table_name',
        width: 150,
        placeholder: '表',
        type: 'select',
        list_remote: {
          module: 'sdp_workspace', method:'get_table_list',
          mode: 'dynamic',
          query: {},
          sortInfo: [],
          mapInfo: {label:'name', value:'name'}
        },
      },
      {
        prop: 'parameter_catalog',
        width: 150,
        type: 'select',
        list: [
          { label: 'SQL', value: 'sql' },
          { label: 'SQL参数-sql.param', value: 'sql.param' },
          { label: 'SQL结果扩展属性-sql.reponse', value: 'sql.response' },
          { label: '自定义方法-api.facade', value: 'api.facade' },
          { label: '内置方法请求扩展属性-api.request', value: 'api.request' },
          { label: '内置方法结果扩展属性-api.response', value: 'api.response' },
        ],
        onchange: param => {
          let {mode, form, column, columns, get_remote_list, set_query_storage} = param
          if (!form) {
            //TODO 为什么会调用到这里？？？
            console.error('param', param)
            return;
          }
          let map = {
            sql: [
              'parameter_sql',
              'is_interface',
              'is_nolist',
              'is_update',
              'is_frontend_list',
              'url',
              'java_return_type',
              'java_imports',
              // 'parameter_sql_issimple',
            ],
            'sql.param': [
              'name',
              'java_type',
              'is_collection',
              'sql_replace_line',
              'parameter_nullable',
              'parameter_is_like',
              'parameter_sql_value',
              'parameter_sql_value_ignore',
              'parameter_without_test',
              'java_imports',
              'request_json_name',
            ],
            'sql.response': [
              'name',
              'java_type',
              'java_imports',
              'response_json_name',
              'response_default_value',
            ],
            'api.facade': [
              'name',
              'java_type',
              'java_imports',
              'parameter_is_import_excel',
              'parameter_is_export_excel',
              'java_return_type',
              'request_extra_param',
              'request_extra_param_simple',
              'request_extra_param_web',
              'url',
            ],
            'api.response': [
              'name',
              'java_type',
              'java_imports',
              'response_json_name',
            ],
            'api.request': [
              'name',
              'java_type',
              'java_imports',
              'parameter_nullable',
              'request_json_name',
            ],
          }
          let list = map[form[column.prop]];
          unref(self.editColumns).forEach(ele => {
            if (ele.prop == 'id'
              || ele.prop == 'workspace_name'
              || ele.prop == 'table_name'
              || ele.prop == 'parameter_catalog'
              || ele.prop == 'parameter_catalog_type'
              || ele.prop == 'extra_info'
              || ele.prop == 'remarks'
              || ele.prop == 'remark'
              || ele.prop == 'sort_no'
            ) {
              return;
            }
            let p = ele.prop
            let extraP = 'extra_info_'+p
            if (self.extra_info_columns.find(e => e.prop == extraP)) {
              p = extraP
            }
            let valid = list.find(e => e == p) || list.find(e => 'extra_info_' + e == p)
            if (valid) {
              ele.hide = false
            } else {
              ele.hide = true
            }
          })
        }
      },
      {
        prop: 'parameter_catalog_type',
        width: 150,
      },
      {
        prop: 'name',
        width: 150,
      },
      {

      },
      {
        prop: 'parameter_is_like',
        type: 'switch',
      },
      {

      },
      {
        prop: 'parameter_without_test',
        type: 'switch',
      },
      {
        prop: 'parameter_sql',
        type: 'textarea',
        rows: 8,
      },
      {
        prop: 'parameter_nullable',
        type: 'switch',
      },
      {
        prop: 'parameter_sql_value_ignore',
        type: 'switch',
      },
      {
        prop: 'java_imports',
        type: 'textarea',
      },
      {
        prop: 'java_return_type',
      },
      {
        prop: 'is_interface',
        type: 'switch',
      },
      {
        prop: 'extra_info',
        type: 'textarea',
      },
      ...self.extra_info_columns.map(ele => {
        let onchange = param => {
          let {mode, form, column, columns, get_remote_list, set_query_storage} = param
          let extra_info
          if (form.extra_info) {
            extra_info = JSON.parse(form.extra_info)
          } else {
            extra_info = {}
          }
          let prop = column.prop.substring('extra_info_'.length)
          if (form[column.prop]) {
            extra_info[prop] = form[column.prop]
          } else {
            delete extra_info[prop]
          }
          form.extra_info = JSON.stringify(extra_info)
        }
  
        if (ele.prop.indexOf('is_') >= 0) {
          return {
            ...ele,
            prop: 'extra_info_'+ele.prop,
            type: 'switch',
            onchange,
          }
        }
        return {
          ...ele,
          prop: 'extra_info_'+ele.prop,
          onchange,
        }
      }),
      {

      },
      {
        prop: 'parameter_is_import_excel',
        type: 'switch',
      },
      {
        prop: 'parameter_is_export_excel',
        type: 'switch',
      },
      {
        prop: 'sort_no',
      }
    ],

    queryColumns : [
      {
        prop: 'workspace_name',
        fixed: true,
        list_remote: {
          module: 'sdp_workspace', method:'list',
          query: {},
          sortInfo: ['name'],
          mapInfo: {label:'name', value:'name'},
        },
        type: 'select',
        onchange: param => {
          let {mode, form, column, columns, get_remote_list, set_query_storage} = param
          get_remote_list({...param, columns,
            prop: 'table_name_like', query: {workspace_name: form[column.prop]}})
          .then(param => {
            const { data } = param
            if (data && data.length) {
              let column = columns.find(ele => ele.prop == 'table_name_like')
              column.list.forEach( ele => {
                if (ele.data) {
                  if (ele.data.alias_name) {
                    ele.label += ' as '+ele.data.alias_name
                  }
                  let remark = ele.data.remark
                  if (remark) {
                    remark += ''
                    remark = remark.split('\n')[0].split('\r')[0].trim()
                    if (remark.length > 15) {
                      remark = remark.substring(0, 15)+'...'
                    }
                    ele.label += ' ('+remark+')'
                  }
                }
              })
              if (form.table_name_like && !data.find(ele => ele.name == form.table_name_like)) {
                form.table_name_like = ''
              }
            }
          })
        },
      },
      {
        prop: 'table_name_like',
        label: '表',
        width: 150,
        type: 'select',
        list_remote: {
          module: 'sdp_workspace', method:'get_table_list',
          mode: 'dynamic',
          query: {},
          sortInfo: [],
          mapInfo: {label:'name', value:'name'}
        },
      },
      {
        prop: 'parameter_catalog_type_like',
        label: '方法名',
        width: '150px',
      },
      {
        prop: 'name_like',
        label: '名称',
        width: '150px',
      },
      {
        prop: 'parameter_sql_like',
        label: 'SQL',
        width: '150px',
      },
      {
        prop: 'extra_info_like',
        label: '扩展属性',
        width: '150px',
      },
    ],

    buttons: [
      {
        prop: 'fireExecute',
        label: '生成文件',
        loading: false,
        type: 'success',
        onclick: (param) => {
          fireExecute({param, self})
        }
      },
    ],

    remoteConfig : {
    },

    onInit: () => {
      self.handleSearch_ori = self.handleSearch
      self.handleSearch = (param) => {
        return self.handleSearch_ori(param)
        .then( data => {
          unref(self.tableData).forEach( ele => {
            if (ele.extra_info) {
              let j = JSON.parse(ele.extra_info)
              for(let p in j) {
                ele['extra_info_'+p] = j[p]
              }
            }
          })
          return data;
        })
      }
    },

  }
}

